/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import business.PlacesBusiness;
import business.TourBusiness;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import model.Hotels;
import model.Locations;
import model.PackageTour;
import model.PackageType;
import model.Places;
import javax.servlet.http.HttpSession;
import model.PackageTourDetails;

/**
 *
 * @author Nhang Con
 */
@ManagedBean
@RequestScoped
public class TourBean {

    private int packageID;
    private int locationID;
    private int placeID;
    private int hotelID;
    private List<Hotels> lhotel;
    private List<Places> lplaces;
    private List<Locations> llocation;
    private List<PackageType> lpackagetype;
    private Date fdate;
    private Date edate;
    private int pageID;
    private Date datenow;
    private PackageTour ptour;
    private List<PackageTour> lAllPackageTour;

    public List<PackageTour> getlAllPackageTour() {
        return lAllPackageTour;
    }

    public PackageTour getPtour() {
        return ptour;
    }

    public void setPtour(PackageTour ptour) {
        this.ptour = ptour;
    }

    public Date getDatenow() {
        return datenow;
    }

    public void setDatenow(Date datenow) {
        this.datenow = datenow;
    }
    private List<Integer> pageList;

    public int getPageID() {
        return pageID;
    }

    public void setPageID(int pageID) {
        this.pageID = pageID;
    }

    public List<Integer> getPageList() {
        return pageList;
    }

    public void setPageList(List<Integer> pageList) {
        this.pageList = pageList;
    }

    public Date getEdate() {
        return edate;
    }

    public void setEdate(Date edate) {
        this.edate = edate;
    }

    public Date getFdate() {
        return fdate;
    }

    public void setFdate(Date fdate) {
        this.fdate = fdate;
    }

    public List<Hotels> getLhotel() {
        return lhotel;
    }

    public void setLhotel(List<Hotels> lhotel) {
        this.lhotel = lhotel;
    }

    public List<Locations> getLlocation() {
        return llocation;
    }

    public void setLlocation(List<Locations> llocation) {
        this.llocation = llocation;
    }

    public List<PackageType> getLpackagetype() {
        return lpackagetype;
    }

    public void setLpackagetype(List<PackageType> lpackagetype) {
        this.lpackagetype = lpackagetype;
    }

    public List<Places> getLplaces() {
        return lplaces;
    }

    public void setLplaces(List<Places> lplaces) {
        this.lplaces = lplaces;
    }

    public int getHotelID() {
        return hotelID;
    }

    public void setHotelID(int hotelID) {
        this.hotelID = hotelID;
    }

    public int getLocationID() {
        return locationID;
    }

    public void setLocationID(int locationID) {
        this.locationID = locationID;
    }

    public int getPackageID() {
        return packageID;
    }

    public void setPackageID(int packageID) {
        this.packageID = packageID;
    }

    public int getPlaceID() {
        return placeID;
    }

    public void setPlaceID(int placeID) {
        this.placeID = placeID;
    }
    TourBusiness tb = new TourBusiness();

    /** Creates a new instance of TourBean */
    public TourBean() {

        ptour = new PackageTour();
        datenow = new Date();
//        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
//       datenow= (Date) sdf.parse(null)
        lpackagetype = tb.getListPType();
        llocation = tb.getListLocation();
        locationID = 1;
        placeID = 1;
        lplaces = placesBusiness.listPlacesByLocalID(locationID);
        lhotel = tb.getListHotelByPlace(placeID);
        int pageIndex = tb.getIndexTour();
        pageList = new ArrayList<Integer>();
        if (pageIndex % 3 == 0) {
            for (int i = 1; i <= pageIndex / 3; i++) {
                pageList.add(i);
            }
        } else {
            for (int i = 1; i <= pageIndex / 3 + 1; i++) {
                pageList.add(i);
            }
        }

        if (FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("pageID1") != null) {
            pageID = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("pageID1").toString());
        } else {
            pageID = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("PageID2").toString());
        }
        lAllPackageTour = tb.getListPKTByPage(pageID);
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        session.setAttribute("PageID2", pageID);


    }

    private String formatDateFrom(Date dateFrom) {
        SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
        String dateFromString = formatter.format(dateFrom);
        return dateFromString;
    }
    private PlacesBusiness placesBusiness = new PlacesBusiness();

    public void placeChangeByID(ValueChangeEvent e) {
        locationID = Integer.parseInt(e.getNewValue().toString());
        lplaces = placesBusiness.listPlacesByLocalID(locationID);
        int pID = lplaces.get(0).getPlaceID();
        lhotel = tb.getListHotelByPlace(pID);

    }

    public void hotelChangeByID(ValueChangeEvent e) {
        placeID = Integer.parseInt(e.getNewValue().toString());
        lhotel = tb.getListHotelByPlace(placeID);

    }

    public String createTour() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        if (session.getAttribute("sessionPathFile") != null) {
            String path = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("sessionPathFile").toString();
            String getfile = "images/upload/" + path;
            ptour.setImages(getfile);
            ptour.setPackageTypeID(packageID);
            ptour.setHotelID(hotelID);
            ptour.setLocationID(locationID);
            ptour.setPlaceID(placeID);

            if (fdate.before(edate)) {
                ptour.setFromdate(formatDateFrom(fdate));
                ptour.setEnddate(formatDateFrom(edate));
                if (tb.newTour(ptour)) {
                    session.setAttribute("sessionPathFile", null);
                    return "managepackagetour.xhtml?pageID1=1";
                } else {
                    session.setAttribute("sessionPathFile", null);
                    return "error.xhtml";
                }
            }
            return "error.xhtml";
        }
        return "error.xhtml";
    }

    public String actionUpdateTour(PackageTour pktour) {
        if (tb.updatePTour(pktour)) {
            return "managepackagetour.xhtml?pageID1=1";
        }
        return "error.xhtml";


    }
}
